﻿using System;
using System.Collections.Generic;
using System.Data;
using RpcHelper;
using RpcModel;

using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class RemoteServerConfigDAL : SqlBasicClass
        {
                public RemoteServerConfigDAL () : base ("RemoteServerConfig")
                {

                }
                public bool CheckIsExists (long sysTypeId)
                {
                        return base.CheckIsExists (new ISqlWhere[]{
                                new SqlWhere("SystemType", SqlDbType.BigInt) { Value = sysTypeId }
                        });
                }
                public long Add (ServerConfigAddParam add)
                {
                        base.Insert (add, out long id);
                        return id;
                }
                public int GetServiceIndex (long sysTypeId, string mac)
                {
                        return base.ExecuteScalar<int> ("ServerIndex", SqlFuncType.max, new ISqlWhere[] {
                                new SqlWhere("SystemType", SqlDbType.BigInt){Value=sysTypeId},
                                new SqlWhere("ServerMac", SqlDbType.VarChar,17){Value=mac}
                        });
                }

                internal void SetState (long id, RpcServiceState state)
                {
                        if (!base.Update (new ISqlSetColumn[] {
                            new SqlSetColumn("ServiceState", SqlDbType.SmallInt){Value=state}
                       }, "Id", id))
                        {
                                throw new ErrorException ("rpc.service.state.set.error");
                        }
                }

                public void SetService (long id, ServerConfigSetParam data)
                {
                        if (!base.Update (data, "Id", id))
                        {
                                throw new ErrorException ("rpc.service.set.error");
                        }
                }
                public void Delete (long id)
                {
                        if (!base.Drop ("Id", id))
                        {
                                throw new ErrorException ("rpc.service.drop.error");
                        }
                }
                public ServerConfigDatum[] Query (QueryServiceParam query, IBasicPage paging, out long count)
                {
                        paging.InitOrderBy ("Id", true);
                        return base.Query<ServerConfigDatum> (paging.OrderBy, paging.Index, paging.Size, out count, query.ToWhere ());
                }
                public ServerConfigDatum Get (long id)
                {
                        return base.GetRow<long, ServerConfigDatum> ("Id", id);
                }
                public ServerConfigDatum[] GetServices (long[] ids)
                {
                        return base.Get<long, ServerConfigDatum> ("Id", ids);
                }
                public bool CheckIsOnline (long id)
                {
                        return base.ExecuteScalar<bool> ("IsOnline", new SqlWhere ("Id", SqlDbType.BigInt) { Value = id });
                }

                public bool CheckServerPort (string mac, int serverPort)
                {
                        return base.CheckIsExists (new ISqlWhere[] {
                                new SqlWhere("ServerMac", SqlDbType.VarChar,17){Value=mac},
                                new SqlWhere("ServerPort", SqlDbType.Int){Value=serverPort}
                        });
                }

                internal bool CheckIsExistsByGroup (long groupId)
                {
                        return base.CheckIsExists (new ISqlWhere[]{
                                new SqlWhere("GroupId", SqlDbType.BigInt) { Value = groupId }
                        });
                }

                internal bool CheckRegion (int regionId)
                {
                        return base.CheckIsExists (new ISqlWhere[]{
                                new SqlWhere("RegionId", SqlDbType.Int) { Value = regionId }
                        });
                }

                internal BasicService[] GetBasic (long[] ids)
                {
                        return base.Get<long, BasicService> ("Id", ids);
                }
        }
}
